<h1 id="变量作用域">变量作用域</h1>
<p><code>Quick</code>是允许变量不用显示定义(声明)的，首次赋值会自动定义。变量作用域规则如下：</p>
<ul>
<li>若变量在当前作用域没定义， 父级作用域已存在变量， 则使用父级作用域的变量</li>
<li>当前作用域与父级作用域存在相同名字的变量， 则使用当前作用域的变量</li>
<li>如果变量在所有作用域未定义，默认定义在当前作用域</li>
<li>当父级作用域已定义该变量，可以使用关键字<code>var</code>让变量定义在当前作用域</li>
<li>for/foreach 会默认使用当前作用域</li>
</ul>
<pre><code class="hljs language-javascript">
a, b = <span class="hljs-number">998</span>, <span class="hljs-string">&quot;tom&quot;</span>

<span class="hljs-title function_">echo</span>(a, b, <span class="hljs-string">&quot;start&quot;</span>)

<span class="hljs-title function_">demo</span>(<span class="hljs-params"></span>) {
    <span class="hljs-keyword">var</span> a, b = <span class="hljs-literal">true</span>, <span class="hljs-title function_">now</span>()
    <span class="hljs-title function_">echo</span>(a, b)
}
<span class="hljs-title function_">demo2</span>(<span class="hljs-params"></span>) {
    <span class="hljs-title function_">echo</span>(<span class="hljs-string">&quot;-----------------&quot;</span>)
    <span class="hljs-title function_">echo</span>(a, b, <span class="hljs-string">&quot;20&quot;</span>)
    <span class="hljs-keyword">var</span> a, b
    <span class="hljs-title function_">echo</span>(a, b, <span class="hljs-string">&quot;21&quot;</span>)
    a = <span class="hljs-number">999</span>
    b = <span class="hljs-number">111</span>
    <span class="hljs-title function_">echo</span>(a, b, <span class="hljs-string">&quot;29&quot;</span>)
    <span class="hljs-title function_">echo</span>(<span class="hljs-string">&quot;-----------------&quot;</span>)
}
<span class="hljs-title function_">demo3</span>(<span class="hljs-params"></span>) {
    <span class="hljs-title function_">echo</span>(<span class="hljs-string">&quot;-----------------&quot;</span>)
    <span class="hljs-title function_">echo</span>(a, b, <span class="hljs-string">&quot;demo3&quot;</span>)
    <span class="hljs-keyword">var</span> a = <span class="hljs-string">&quot;xxx&quot;</span>
    <span class="hljs-keyword">var</span> b = <span class="hljs-string">&quot;AAA&quot;</span>
    <span class="hljs-title function_">echo</span>(a, b, <span class="hljs-string">&quot;demo3&quot;</span>)
    <span class="hljs-title function_">echo</span>(<span class="hljs-string">&quot;-----------------&quot;</span>)
}

<span class="hljs-title function_">demo</span>()
<span class="hljs-title function_">demo2</span>()
<span class="hljs-title function_">demo3</span>()
<span class="hljs-title function_">echo</span>(a, b, <span class="hljs-string">&quot;finally&quot;</span>)
</code></pre>
